/**
 * @requires jquery.validate.js
 * @author ZhangHuihua@msn.com
 */
(function($) {
	if ($.validator) {
		jQuery.validator.addMethod("refer",function(value, element, params){
			var refer = $(element).attr("refer");
			var v = parseInt($("input#" + refer).val());
			value = parseInt(value);
			if (!v || v== "") {
				return false;
			} else {
				return true;
			}
		},jQuery.validator.format("必填选项"));
		jQuery.validator.addMethod("url",function(value,element){
			 if(!value || value.startsWith("http://") || value.startsWith("xunbaozl://")) {
				 return true;
			 }else {
				 return false;
			 }
		},jQuery.validator.format("链接格式不正确"));
		jQuery.validator.addMethod("lt", function(value, element, params) {
			var lt = $(element).attr("lt");
			var v = parseInt($("input#" + lt).val());
			value = parseInt(value);
			if (v <= value) {
				return false;
			} else {
				return true;
			}
		}, jQuery.validator.format("超出范围"));
		/*jQuery.validator.addMethod("remote",function(value, element, params){
			var url $(element).attr("validateUrl");
			var data = {"value":value};
			var result = false;
			if(!url || url == "") {
				return false;
			}else {
				jQuery.ajax({
					url:url,
					async:false,
					data:data,
					success:function(data, textStatus, jqXHR){
						if(data === "true") {
							result = true;
						}
					}				
				});
			}
			return result;
		},jQuery.validator.format("错误"));*/
		$.validator.addMethod("alphanumeric", function(value, element) {
			return this.optional(element) || /^\w+$/i.test(value);
		}, "Letters, numbers or underscores only please");

		$.validator.addMethod("lettersonly", function(value, element) {
			return this.optional(element) || /^[a-z]+$/i.test(value);
		}, "Letters only please");

		$.validator.addMethod("phone", function(value, element) {
			return this.optional(element) || /^[0-9 \(\)]{7,30}$/.test(value);
		}, "Please specify a valid phone number");

		$.validator.addMethod("postcode", function(value, element) {
			return this.optional(element) || /^[0-9 A-Za-z]{5,20}$/.test(value);
		}, "Please specify a valid postcode");

		$.validator.addMethod("date", function(value, element) {
			value = value.replace(/\s+/g, "");
			if (String.prototype.parseDate) {
				var $input = $(element);
				var pattern = $input.attr('dateFmt') || 'yyyy-MM-dd';

				return !$input.val() || $input.val().parseDate(pattern);
			} else {
				return this.optional(element) || value
					.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/);
			}
		}, "Please enter a valid date.");

		/*
		 * 自定义js函数验证 <input type="text" name="xxx" customvalid="xxxFn(element)"
		 * title="xxx" />
		 */
		$.validator.addMethod("customvalid", function(value, element, params) {
			try {
				return eval('(' + params + ')');
			} catch (e) {
				return false;
			}
		}, "Please fix this field.");

		$.validator.addClassRules({
			date: {
				date: true
			},
			alphanumeric: {
				alphanumeric: true
			},
			lettersonly: {
				lettersonly: true
			},
			phone: {
				phone: true
			},
			postcode: {
				postcode: true
			}
		});
		$.validator.setDefaults({
			errorElement: "span"
		});
		$.validator.autoCreateRanges = true;

	}

})(jQuery);